package com.atguigu.crowd.mvc.handler;

import com.atguigu.crowd.entity.Admin;
import com.atguigu.crowd.service.api.AdminService;
import com.atguigu.crowd.util.CrowdConstant;
import com.atguigu.crowd.util.CrowdUtil;
import com.atguigu.crowd.util.ResultEntity;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Controller
@RequestMapping("/admin/authority/user")
public class AdminHandler {

    @Autowired
    private AdminService adminService;

    @Autowired
    private PasswordEncoder passwordEncoder;

    /**
     * 权限管理，用户列表页面
     *
     * @param keyword
     * @param pageNum
     * @param pageSize
     * @param modelMap
     * @return
     */
    @RequestMapping(value = "/list")
    public String getUserListPage(@RequestParam(value = "keyword", defaultValue = "") String keyword,
                               @RequestParam(value="pageNum", defaultValue="1") Integer pageNum,
                               @RequestParam(value="pageSize", defaultValue="10") Integer pageSize,
                               ModelMap modelMap) {
        PageInfo<Admin> pageInfo = adminService.getAdminPage(keyword, pageSize, pageNum);
        modelMap.addAttribute(CrowdConstant.ATTR_NAME_PAGE_INFO, pageInfo);
        return "admin-authority-user";
    }

    @GetMapping("/add")
    public String getUserAdd() {
        return "admin-authority-user-add";
    }

    @PostMapping("/add")
    public String postUserAdd(Admin admin) {
        adminService.saveAdmin(admin);
        return "redirect:/admin/authority/user/list";
    }

    @ResponseBody
    @PostMapping("/delete/{adminId}")
    public ResultEntity deleteUser(@PathVariable("adminId") Integer adminId) {
        adminService.removeAdmin(adminId);
        return ResultEntity.successWithoutData();
    }

    @GetMapping("/update/{adminId}")
    public String getUpdateUser(@PathVariable("adminId") Integer adminId, ModelMap modelMap) {
        Admin admin = adminService.getAdminById(adminId);
        modelMap.addAttribute("admin", admin);
        return "admin-authority-user-edit";
    }

    @PostMapping("/update")
    public String postUpdateUser(Admin admin) {
        String pswd = admin.getUserPswd();
        if (!StringUtils.isEmpty(pswd)) {
//            admin.setUserPswd(CrowdUtil.md5(pswd));
            admin.setUserPswd(passwordEncoder.encode(pswd));
        }
        adminService.updateAdmin(admin);
        return "redirect:/admin/authority/user/list";
    }

    @RequestMapping("/role/assign")
    public String saveRoleRelation(@RequestParam("adminId") Integer adminId,
                                   @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                                   @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize,
                                   @RequestParam(value = "keyword", defaultValue = "") String keyword,
                                   @RequestParam(name = "assignRoleList", required = false) List<Integer> roleIdList,
                                   ModelMap modelMap) {
        System.out.println(roleIdList);
        adminService.saveRoleRelationship(adminId, roleIdList);
        modelMap.addAttribute(CrowdConstant.ATTR_NAME_TIPS,"操作成功");
        return getUserListPage(keyword, pageNum, pageSize, modelMap);
    }
}
